//
//  CYGDetailViewController.m
//  CYGHuntForCity
//
//  Created by cygarm on 16/3/17.
//  Copyright © 2016年 cygarm. All rights reserved.
//

#import "CYGDetailViewController.h"
#import "UIViewController+CYGPushedNaviBar.h"
#import "CYGDetailNaviBackView.h"
#import "CYGDetailSelectView.h"
#import "CYGDetailRcmdHeader.h"
#import "CYGDetailRcmdFooter.h"
#import "CYGDetailRcmdTableViewController.h"
#import "UIView+CYGEx.h"
#import "CYGDetailModel.h"
#import <MJExtension.h>

static const CGFloat CYGDetailNaviBackViewHeight = 200.0f;
static const CGFloat CYGDetailBeenAndCollectViewHeight = 45.0f;
static const CGFloat CYGDetailSelectViewHeight = 45.0f;

@interface CYGDetailViewController () <UIScrollViewDelegate, CYGDetailSelectViewDelegate, CYGDetailRcmdTableViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *beenBtn;
@property (weak, nonatomic) IBOutlet UIButton *collectBtn;
@property (strong, nonatomic) UIScrollView *backScrollView;
@property (strong, nonatomic) UIView *naviBar;
@property (strong, nonatomic) CYGDetailNaviBackView *naviBackView;
@property (strong, nonatomic) CYGDetailSelectView *selectView;
@property (strong, nonatomic) UITableView *rcmdTableView;
@property (strong, nonatomic) CYGDetailRcmdHeader *rcmdTableViewHeader;
@property (strong, nonatomic) CYGDetailRcmdFooter *rcmdTableViewFooter;
@property (strong, nonatomic) CYGDetailRcmdTableViewController *rcmdTableViewVC;
@property (strong, nonatomic) UIView *naviBackViewCover;

@property (strong, nonatomic) CYGDetailModel *datas;
@end

@implementation CYGDetailViewController {
	CYGDetailSelectViewTag selectViewBtn;
	CGPoint naviBackBiewBasicCenter;
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	[self setupUI];
}

- (void)setupUI {
	self.automaticallyAdjustsScrollViewInsets = NO;
	
	self.backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
	self.backScrollView.pagingEnabled = YES;
	self.backScrollView.bounces = NO;
	self.backScrollView.showsHorizontalScrollIndicator = NO;
	self.backScrollView.showsVerticalScrollIndicator = NO;
	self.backScrollView.scrollEnabled = YES;
	self.backScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
	self.backScrollView.contentSize = CGSizeMake(CYGAppWidth*2, 0);
	self.backScrollView.delegate = self;
	[self.view insertSubview:self.backScrollView atIndex:0];
	
	NSArray<NSString *> *imageNames = @[
	@"http://img.chengmi.com/cm/3bc2198c-c909-4698-91b2-88e00c5dff2a",
	@"http://img.chengmi.com/cm/dba3fb4d-b5ef-4218-b976-52cba4538381",
	@"http://img.chengmi.com/cm/934ad87f-400c-452e-9427-12a03fe9cf6e"];
	self.naviBackView = [CYGDetailNaviBackView viewWithImages:imageNames];
	self.naviBackView.frame = CGRectMake(0, 0, self.view.bounds.size.width, CYGDetailNaviBackViewHeight);
	naviBackBiewBasicCenter = self.naviBackView.center;
	[self.view addSubview:self.naviBackView];
	
	self.naviBackViewCover = [[UIView alloc] initWithFrame:self.naviBackView.frame];
	self.naviBackViewCover.backgroundColor = CYGGolbalGreen;
	self.naviBackViewCover.alpha = 0;
	[self.view addSubview:self.naviBackViewCover];
	
	self.selectView = [[CYGDetailSelectView alloc] init];
	selectViewBtn = CYGDetailSelectViewTagRcmdBtn;
	self.selectView.delegate = self;
	[self.view addSubview:self.selectView];
	
	self.rcmdTableViewVC = [[CYGDetailRcmdTableViewController alloc] init];
	self.rcmdTableViewVC.parent = self;
	self.rcmdTableViewVC.datas = self.datas.article_list[0][@"newcontent"];
	self.rcmdTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.backScrollView.bounds.size.width, self.backScrollView.bounds.size.height)];
	self.rcmdTableView.contentInset = UIEdgeInsetsMake(CYGDetailNaviBackViewHeight+CYGDetailSelectViewHeight, 0, CYGDetailBeenAndCollectViewHeight, 0);
	self.rcmdTableViewHeader = [[CYGDetailRcmdHeader alloc] init];
	self.rcmdTableView.tableHeaderView = self.rcmdTableViewHeader;
	self.rcmdTableViewFooter = [[CYGDetailRcmdFooter alloc] init];
	self.rcmdTableView.tableFooterView = self.rcmdTableViewFooter;
	self.rcmdTableView.delegate = self.rcmdTableViewVC;
	self.rcmdTableView.dataSource = self.rcmdTableViewVC;
	self.rcmdTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
	[self.backScrollView addSubview:self.rcmdTableView];
	
	self.naviBar = [self cyg_customPushedNavigationBarWithBackgroundColor:[UIColor clearColor] titile:@"嘟嘟" subTitle:nil];
	[self.view addSubview:self.naviBar];	
}

- (void)viewWillLayoutSubviews {
	self.selectView.frame = CGRectMake(0, CYGViewBottom(self.naviBackView), self.view.bounds.size.width, CYGDetailSelectViewHeight);
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
	[self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CYGDetailModel *)datas {
	if (_datas == nil) {
		NSString *path = [[NSBundle mainBundle] pathForResource:@"detailJson" ofType:nil];
		NSData *data = [NSData dataWithContentsOfFile:path];
		NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
		
		_datas = [CYGDetailModel mj_objectWithKeyValues:jsonDict[@"body"]];
	}
	return _datas;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - IBAction

- (IBAction)beenBtnClick:(id)sender {
}

- (IBAction)collectBtnClick:(id)sender {
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//	DLog(@"scrollView.contentOffset=(%f,%f)", scrollView.contentOffset.x, scrollView.contentOffset.y);
	
	CGFloat pageWidth = scrollView.bounds.size.width;
	int page = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
	CYGDetailSelectViewTag btn = (CYGDetailSelectViewTag)(page+1);
	if (btn != selectViewBtn) {
		[self.selectView selectBtn:btn];
	}
}

- (void)detailRcmdTableViewDidScroll:(CGPoint)offset {
	static CGFloat lastY = 0;
	CGFloat y = offset.y+CYGDetailNaviBackViewHeight+CYGDetailSelectViewHeight;
	CGRect selectViewFrame = self.selectView.frame;
	CGRect naviBackViewFrame = self.naviBackView.frame;
	CGRect naviBackViewBounds = self.naviBackView.bounds;
	
	selectViewFrame.origin.y = -offset.y-self.selectView.bounds.size.height;
	CGFloat minY = CYGViewBottom(self.naviBar);
	if (selectViewFrame.origin.y < minY) {
		selectViewFrame.origin.y = minY;
	}
	self.selectView.frame = selectViewFrame;
	
	if (lastY < 0 && y > 0) {
		naviBackViewBounds = [self.naviBackView cyg_viewScale:1.0f];
	}
	
	if (y <= 0) {
		CGFloat scale;
		scale = 1-y/100;
		scale = scale > 1 ? scale : 1;
		naviBackViewBounds = [self.naviBackView cyg_viewScale:scale];
		self.naviBackView.center = naviBackBiewBasicCenter;
	}
	if (y > 0) {
		naviBackViewFrame.origin.y = self.selectView.frame.origin.y-naviBackViewBounds.size.height;
		self.naviBackView.frame = naviBackViewFrame;
		self.naviBackViewCover.frame = naviBackViewFrame;
		CGFloat coverMaxY = fabsf(self.naviBackViewCover.bounds.size.height-self.naviBar.bounds.size.height);
		CGFloat coverMinY = self.naviBackViewCover.bounds.size.height/2;
		CGFloat coverY = fabsf(naviBackViewFrame.origin.y);
		if (coverY <= coverMaxY && coverY >= coverMinY) {
			self.naviBackViewCover.alpha = (coverY-coverMinY)/(coverMaxY-coverMinY);
		} else {
			self.naviBackViewCover.alpha = 0;
		}
	}
}

#pragma mark - CYGDetailSelectViewDelegate

- (void)detailSelectViewBtnClick:(CYGDetailSelectViewTag)tag {
//	DLog(@"tag=%d", tag);
	if (selectViewBtn != tag) {
		CGPoint offset = self.backScrollView.contentOffset;
		offset.x = self.backScrollView.bounds.size.width*(tag-1);
		[UIView animateWithDuration:0.3f animations:^{
			self.backScrollView.contentOffset = offset;
		}];
		selectViewBtn = tag;
	}
}

@end
